package org.wmq.travelagency02.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.common.UserContext;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.model.Entity.Traveller;
import org.wmq.travelagency02.service.TravellerService;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/traveller")
@RequiredArgsConstructor
public class TravellerController {
    @Autowired
    private TravellerService travellerService;

    @GetMapping("/{id}")
    public Result<Traveller> getById(@PathVariable Long id){
        Traveller result = this.travellerService.getById(id);
        if (result == null){
            return Result.error("用户不存在");
        }
        return Result.ok(result);
    }

    @GetMapping("/me")
    public Result<Traveller> me(){
        return this.getById(UserContext.getUserId());
    }

    @GetMapping("/getByName/{name}")
    public Result<Traveller> getByName(@PathVariable String name){
        Traveller result = this.travellerService.lambdaQuery().
                eq(Traveller::getName, name).
                select(Traveller::getPhone
                        , Traveller::getAge, Traveller::getGender,
                        Traveller::getId).
                one();
        if (result == null){
            return Result.error("用户不存在");
        }
        result.setName(name);
        return Result.ok(result);
    }

    @GetMapping("/getByPhone/{phone}")
    public Result<Traveller> getByPhone(@PathVariable String phone){
        Traveller result = this.travellerService.lambdaQuery().
                eq(Traveller::getPhone, phone).
                select(Traveller::getName,Traveller::getId
                        , Traveller::getAge, Traveller::getGender,
                        Traveller::getId).
                one();
        if (result == null){
            return Result.error("用户不存在");
        }
        result.setPhone(phone);
        return Result.ok(result);
    }


    @PutMapping
    public Result<Void> update(@RequestBody Traveller traveller){
        traveller.setUpdateTime(LocalDateTime.now());
        if(this.travellerService.updateById(traveller)){
            return Result.ok();
        }
        return Result.error("用户更新失败");
    }

    @PostMapping
    public Result<Void> save(@RequestBody Traveller traveller){
        traveller.setCreateTime(LocalDateTime.now());
        traveller.setUpdateTime(LocalDateTime.now());
        if (this.travellerService.save(traveller)){
            return Result.ok();
        }
        return Result.error("用户保存失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id){
        if (this.travellerService.removeById(id)){
            return Result.ok();
        }
        return Result.error("用户删除失败");
    }

    @GetMapping("/getByChatUserId/{chatUserId}")
    public Result<Traveller> getByChatUserId(@PathVariable String chatUserId){
        Traveller result = this.travellerService.lambdaQuery().
                eq(Traveller::getChatUserId, chatUserId).
                select(Traveller::getName,Traveller::getId
                        , Traveller::getAge, Traveller::getGender).
                one();
        if (result == null){
            return Result.error("用户不存在");
        }
        result.setChatUserId(chatUserId);
        return Result.ok(result);
    }
}
